<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        :permission="permissionList"
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :before-open="beforeOpen"
      >
        <template slot="menuLeft">
          <el-button
            type="primary"
            size="small"
            @click="addsObj"
            >添加</el-button
          >
          <el-button
            type="primary"
            size="small"
            @click="delBatchs"
            >删除</el-button
          >
          <el-button type="primary" size="small" @click="handleExport"
            >导 出</el-button
          >
        </template></avue-crud
      >
      <open-dialog
        :DialogVisible.sync="addVisableDialog"
        title="添加"
        width="60%"
      >
        <el-form label-width="60px" v-if="addVisableDialog">
          <avue-form ref="form" 
            v-model="formAdd"
            :option="tableOptionAdd"
            style="margin-bottom: 40px"
          ></avue-form>
          <div class="bottom-menu">
            <el-button type="primary" @click="save">保存</el-button>
            <el-button type="primary" @click="addVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
    </basic-container>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import baseCrud from "@/mixins/base-crud";
import { dateFormat } from "@/util/date";
import { tableOptionAdd } from "@/const/crud/subwaystm/swstconductoradvancepayment";
import { addObj, delBatch } from "@/api/subwaystm/swstconductoradvancepayment";
import { getByUserCode } from "@/api/admin/user";

export default {
  name: "swstconductoradvancepayment",
  mixins: [baseCrud("subwaystm/swstconductoradvancepayment")],
  data() {
    return {
      formAdd: {},
      tableOptionAdd: tableOptionAdd,
      addVisableDialog: false,
      deviceOperatorNum: "",
    };
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.swstconductoradvancepayment_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.swstconductoradvancepayment_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.swstconductoradvancepayment_edit,
          false
        )
      };
    },
  },
  methods: {
    beforeOpen(done, type) {
      if (type == "add") {
        // this.$nextTick(() =>{
        //   this.form.handTimeDTO = dateFormat(new Date())
        // })
      }
      done();
    },

    addsObj: function () {
      for (var i in this.formAdd) {
        this.formAdd[i] = null;
      }
      let handTime = new Date();
      if (handTime.getHours() < 6) {
        handTime.setDate(handTime.getDate() - 1);
      }
      setTimeout(() => {
        this.formAdd.handTime =
          dateFormat(handTime).substr(0, 10) + " 02:00:00";
      }, 0);
      this.addVisableDialog = true;
    },
    async getUserDeviceOperateNo(parames) {
      await getByUserCode(parames).then((response) => {
        this.deviceOperatorNum = response.data.data.deviceOperateNo;
      });
    },
    save: function () {
      this.$refs.form.validate(vaild=>{
        if(vaild){
            this.getUserDeviceOperateNo(this.formAdd.conductorPersonJobNo).then(
              () => {
                if (this.formAdd.deviceOperatorNum != this.deviceOperatorNum) {
                  this.$confirm(
                    "请检查所选择的设备操作号与该售票员不匹配，是否确定保存？",
                    "提示",
                    {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",
                    }
                  ).then(() => {
                    addObj(this.formAdd)
                      .then((data) => {
                        this.$message.success("添加成功");
                        this.getList(this.page);
                        this.addVisableDialog = false;
                        done();
                      })
                      .catch(() => {});
                  });
                } else {
                  addObj(this.formAdd)
                    .then((data) => {
                      this.$message.success("添加成功");
                      this.getList(this.page);
                      this.addVisableDialog = false;
                      done();
                    })
                    .catch(() => {});
                }
              }
            );
          }
        })
    },
    delBatchs: function () {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        ids.push(table.id);
      }
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delBatch(ids)
          .then((data) => {
            this.$message.success("删除成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {});
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.bottom-menu {
  margin-top: -40px;
  text-align: center;
}
</style>
